import { Cart_model } from '../cart/cart_model.js';

var cart_model = new Cart_model();

Page({
  data: {
    loadingHidden:false,
    selectedTypeCounts:0,
    selectedCounts:0
  },

  onLoad: function (options) {
    
  },

  /*生命周期函数,用来监听页面显示,每次页面打开都会调用一次.*/
  onShow: function() {
    var cartData = cart_model.getCarts();
    var cartCounts = cart_model.getCartCount(true);
    var accountInfo = this._getAccountInfo(cartData);

    console.log(cartCounts);
    this.setData({
      selectedCounts: cartCounts.totalCount,
      selectedTypeCounts: cartCounts.productCount,
      cartData:cartData,
      //订单总价
      account: accountInfo.account,
      loadingHidden: true
    });
  },

  delete:function(event) {
    var id = cart_model.getDataSet(event, 'id');
    var cartList = this.data.cartData;

    for (var key in cartList) {
      if (cartList[key].id == id) {
        cartList.splice(key, 1);
      }

      this.data.cartData = cartList;

      this._updateCart();

      cart_model.delete(id);
    }
  
  },

  _getAccountInfo(cartList) {
    var account = 0;
    var multiple = 100;
    var selectedCounts = 0, selectedTypeCounts=0;

    for (var key in cartList) {
      if (cartList[key].selectStatus) {
        //这里乘以100，不然浮点数相加有BUG
        account += cartList[key].counts * multiple * Number(cartList[key].price) * multiple;
        selectedCounts += cartList[key].counts;
        selectedTypeCounts ++;
      }
    }

    account = account / (multiple * multiple);

    return {
      selectedTypeCounts: selectedTypeCounts,
      selectedCounts: selectedCounts,
      account: account
    }
  },


  //更新购物车信息
  _updateCart: function () {
    var accountInfo = this._getAccountInfo(this.data.cartData);
    this.setData({
      selectedCounts: accountInfo.selectedCounts,
      selectedTypeCounts: accountInfo.selectedTypeCounts,
      cartData: this.data.cartData,
      //订单总价
      account: accountInfo.account
    });
  },

  _getProductIndexById: function(id) {
    var index = -1;
    var cartList = this.data.cartData;
    for (var k in cartList) {
      if (cartList[k].id == id) {
        index = k;
        break;
      }
    }

    return index;
  },

  toggleSelect: function(event) {
    var id = cart_model.getDataSet(event, 'id');
    var status = cart_model.getDataSet(event, 'status');
    var index = this._getProductIndexById(id);
    
    this.data.cartData[index].selectStatus = !status;

    this._updateCart();
  },

  toggleSelectAll:function(event) {
    var status = cart_model.getDataSet(event, 'status')=='true';

    var cartList = this.data.cartData;
    for (var k in cartList) {
        this.data.cartData[k].selectStatus = !status;
    }

    this._updateCart();
  },

  changeCounts:function(event) {
    var countType = cart_model.getDataSet(event, 'type'),
        id = cart_model.getDataSet(event, 'id'),
        index = this._getProductIndexById(id);

    if (index != -1) {
      this.data.cartData[index].selectStatus = !status;
    }

    if (countType == 'cut') {
      cart_model.cutCount(id);
      this.data.cartData[index].counts--;
    } else {
      cart_model.addCount(id);
      this.data.cartData[index].counts++;
    }

    this._updateCart();
  },

  submitOrder: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "萌店",
      desc: "朋友圈的品质生活",
      path: "page/cart/cart"
    }
  }
})